﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using SGCG.Model;

namespace SGCG.Actions
{
    public class CreateFreeCurveAction : Action
    {
        private readonly BorderOptions pallete = new BorderOptions();

        public CreateFreeCurveAction(PellBitmap pellBitmap) : base(pellBitmap) { }

        public override void HandleClick(MouseButtonEventArgs eventArgs, UIElement plotArea)
        {
            Point clickLocation = eventArgs.GetPosition(plotArea);

            int x = (int)(clickLocation.X + 0.5);
            int y = (int)(clickLocation.Y + 0.5);

            if (FreeCurve == null)
            {
                using (new BatchRedrawOperation(PellBitmap))
                {
                    this.PellBitmap.AddObject(FreeCurve = new FreeCurve(null)
                    {
                        X0 = x,
                        Y0 = y,
                        X = x,
                        Y = y,
                    });
                }
            }
            else
            {
                using (new BatchRedrawOperation(PellBitmap))
                {
                    FreeCurve.SetPoint(x, y);
                }
            }
        }

        public override void HandleRightClick(MouseButtonEventArgs eventArgs, UIElement plotArea)
        {
            if (FreeCurve != null)
            {
                FreeCurve = null;
            }
        }

        public override void HandleMouseMove(MouseEventArgs eventArgs, UIElement plotArea)
        {
            if (FreeCurve != null)
            {
                Point mouseLocation = eventArgs.GetPosition(plotArea);
                FreeCurve.X = (int)(mouseLocation.X + 0.5);
                FreeCurve.Y = (int)(mouseLocation.Y + 0.5);
            }
        }

        private FreeCurve freeCurve;
        protected FreeCurve FreeCurve
        {
            get { return freeCurve; }
            set
            {
                freeCurve = value;
                pallete.IHasBorder = value;
            }
        }

        public override UIElement Pallete
        {
            get
            {
                return this.pallete;
            }
        }
    }
}
